<?php
/*
PluginName:日志归档
PluginUrl:http://www.lisijie.org
PluginAuthor:lisijie
PluginAuthorUrl:http://www.lisijie.org
PluginDesc:日志归档widget for Jblog2
PluginVer:2.0.0
*/

class ArchiveWidget extends Widget {
	
	function ArchiveWidget() {
		$this->Widget(array('title'=>'日志归档'));
	}
	
	function setting() {
		echo '<p>标题：<input type="text" name="title" value="'.$this->get_option('title').'" /></p>
		<p>显示数量：<input type="text" name="num" size="5" value="'.$this->get_option('num').'" /></p>';	
	}
	
	function display() {
		if ( !($data = $this->cache->get('widget_archive')) ) {
			$num = $this->get_option('num');
			!$num && $num = 12;
			$data = "<ul>\n";
			$query = $this->db->query("SELECT LEFT(yearmonth,4) AS year, RIGHT(yearmonth,2) AS month, COUNT(*) AS num 
			FROM ".tname('calendar')." GROUP BY yearmonth ORDER BY yearmonth DESC LIMIT {$num}");
			while ( $row = $this->db->fetch_array($query) ) {
				$data .= '<li><a href="'.url("mod=archives&date={$row[year]}-{$row[month]}").'">'.$row['year'].'年'.$row['month'].'月</a> <span>('.$row['num'].')</span></li>'."\n";
			}
			$data .= '<li><a href="'.url('mod=archives').'">更多...</a></li></ul>'."\n";
			$this->cache->set('widget_archive', $data);
		}
		return $data;		
	}
	
	function update($data) {
		$data['num'] = intval($data['num']);
		if ( !$data['num'] ) {
			$data['num'] = 12;
		}
		return $data;
	}
	
	//清除缓存
	function recache() {
		$GLOBALS['cache']->del('widget_archive');
	}
}
add_action('blog_submit', array('ArchiveWidget','recache'));
?>